layui.use(['form', 'table', 'laydate', 'tree'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        tree = layui.tree,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //书籍树
    $.get('/blogSpace/getBookTree',{},function(res){
        if(res.state == "200"){
            layer.msg(res.message, {icon:1});
            //渲染
            var parentTree = tree.render({
                elem: '#bookTree'  //绑定元素
                ,id: 'bookTree'
                ,data: res.treeList
                ,showCheckbox: true
                // ,accordion: true
            });
        }else{
            layer.alert(res.message, {
                icon: 2
            });
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        //书籍判断
        var checkData = tree.getChecked('bookTree');
        if(checkData.length == 0){
            layer.msg('请选择要看的书籍章节', {icon:2});
            return;
        }else if(checkData.length > 1){
            layer.msg('不能选择两本书籍', {icon:2});
            return;
        }

        //时间判断
        var startTime = data.field.startTime.split(" ")[1].split(":");
        var endTime = data.field.endTime.split(" ")[1].split(":");
        if(startTime[0] == "00" || endTime[0] == "00"){
            layer.msg('时间要具体到小时哦！', {icon:2});
            return;
        }

        var bookId = "";
        var directoryName = "";
        var bookListName = "";
        checkData.forEach(function (book) {
            bookId = book.id;
            book.children.forEach(function (directory) {
                directoryName += directory.title + ',';
                directory.children.forEach(function (list) {
                    bookListName += list.title + ',';
                })
            })
        });
        directoryName = directoryName.substring(0, directoryName.length - 1);
        bookListName = bookListName.substring(0, bookListName.length - 1);
        data.field.bookId = bookId;
        data.field.directoryName = directoryName;
        data.field.bookListName= bookListName;

        layer.msg("发布中...", {icon:6});
        var url = "/blogSpace/sendSeeBook";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:6});
                //刷新
                setTimeout(function(){
                    location.reload();
                }, 2000);
            }else{
                layer.msg(res.message, {icon:2});
            }

        });

        layer.close(parentIndex);

        return false;
    });

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });

    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });


    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

});